//引入集合模型
const { collectionModel } = require("../models/collectionModel");






//获取所有的收藏题目
// async function getAllCollection(req,res,next){
//     let collection=await collectionModel.find({}).populate({
//         path:'questions_id',
      
//     }).populate({
//         path:'user_id'
//     })
  
   
//     res.send({code:1,collection})
    

// };

//取消当前的收藏题
async function deleteCollection(req,res,next){
    let {_id}=req.body;
    let delCollection=await collectionModel.deleteMany({_id}).populate({
        path:'questions_id',
    }).populate({
        path:'user_id'
    });
     res.send({code:1,delCollection})
};


//分页
async function getAllCollection (req, res, next) {
    const {pageNum,pageSize} = req.body
    
    // 获取到当前查询的文档所有的总条数
    const total = await collectionModel.countDocuments()
    const pages = Math.ceil(total/pageSize)
    console.log(total);
    let collection = await collectionModel.find({}).populate({
        path:'questions_id',

    }).populate({
        path:'user_id'
    }).skip((pageNum-1)*pageSize).limit(parseInt(pageSize));
    res.send({
      code:1, 
      collection,
      total ,
       pages
    });
  }

module.exports={
    getAllCollection,
    deleteCollection
}